Use case: SMBUS master transmit in interrupt mode

Sequence diagram

SMBUS master transmit in interrupt mode

  @startuml
    title : <font size:28>SMBUS mastert transmit interruption mode

    participant "User Application\n(thread mode)" as appli
    participant "User Application\n(handler mode)" as appli_IRQ
    participant "<font color=green><b>HAL SMBUS</b></font>" as SMBUS
    participant "System HW interruption" as interruption

    group#Linen SMBUS operation

      appli->SMBUS : <font color=green><b>HAL_SMBUS_Master_Transmit_IT()
      return
      SMBUS->SMBUS : Enable SMBUS selected interruptions
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ event
      interruption <[#red]-x? : <font color=red><b>SMBUS IRQ error
      interruption -> appli_IRQ : I2Cx_EV_IRQHandler()
      activate appli_IRQ #DarkSalmon
      appli_IRQ -> SMBUS : HAL_SMBUS_EV_IRQHandler()
      interruption -> appli_IRQ : I2Cx_ER_IRQHandler()
      appli_IRQ -> SMBUS : HAL_SMBUS_ER_IRQHandler()
      return
      deactivate appli_IRQ

      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_MasterTxCpltCallback()
      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_ErrorCallback()
      return
      SMBUS -> appli_IRQ: <font color=green><b>HAL_SMBUS_GetError()
      return

    @enduml

Code snippet

HAL1

HAL2

#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS communication based on IT**** ";

/* Size of SMBUS Transmission buffer */
#define TXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)

void main()
{

  // Add your Init and config code

  /* Start the transmission process */
  HAL_SMBUS_Master_Transmit_IT(&hsmbus2, (uint16_t)SMBUS_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, SMBUS_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/

  while (1)
  {
  }
}

/* SMBUS transfer complete callback */
void HAL_SMBUS_MasterTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
{
  /* Transmission complete */
  // Add your post-transfer code here
}

/* SMBUS error callback */
void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *SMBUSHandle)
{
  if (HAL_SMBUS_GetError(SMBUSHandle) != HAL_SMBUS_ERROR_ACKF)
  {
    /* Handle the error here */
  }
}

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}
#define SMBUS_ADDRESS          0x42

/* Buffer used for SMBUS transmission */
uint8_t aTxBuffer[] = " ****SMBUS communication based on IT**** ";

/* Size of SMBUS Transmission buffer */
#define TXBUFFERSIZE  (COUNTOF(aTxBuffer) - 1)

void main()
{
  // Add your Init and config code

  /* Start the transmission process */
  HAL_SMBUS_MASTER_SEQ_Transmit_IT(&hsmbus2, (uint16_t)SMBUS_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, HAL_SMBUS_XFER_FIRST_AND_LAST_FRAME_NO_PEC);

  /*wait end of SMBUS tranfert*/
  while (hsmbus2.global_state != HAL_SMBUS_STATE_IDLE);

  while (1)
  {
  }
}

/* SMBUS error callback */
void HAL_SMBUS_ErrorCallback(hal_smbus_handle_t *hsmbus2)
{
  if (HAL_SMBUS_GetLastErrorCodes(hsmbus2) != HAL_SMBUS_ERROR_ACKF)
  {
    /* Handle the error here */
  }
}

/* SMBUS event interrupt handler */
void I2C2_EV_IRQHandler(void)
{
  HAL_SMBUS_EV_IRQHandler(&hsmbus2);
}

/* SMBUS error interrupt handler */
void I2C2_ER_IRQHandler(void)
{
  HAL_SMBUS_ER_IRQHandler(&hsmbus2);
}

Key changes

  1. The transmit API changes from HAL_SMBUS_Master_Transmit_IT() to HAL_SMBUS_MASTER_SEQ_Transmit_IT().

  2. Transfer options use the HAL_SMBUS_XFER_* naming scheme in HAL2.

  3. The buffer size parameter is expressed in bytes in HAL2 (Size_byte) instead of the number of data elements in HAL1 (Size).

  4. Error retrieval uses HAL_SMBUS_GetLastErrorCodes() in HAL2.